You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Client: run response continuations on the receive loop thread once it waits (.NET 11 spurious wake-ups) - #5084
A request waits for its response on a ManualResetValueTaskSourceCore with RunContinuationsAsynchronously (the receive loop completes it under the channel gate). The BCL queues that continuation with preferLocal: true, so either the receive loop's thread runs it once its next receive goes pending, or a worker that the enqueue woke takes it. When the loop's thread runs it, the woken worker found no work, and on .NET 11 it spins up to 1024 backoff units before it blocks.
The receive loop now defers the completion of a response (ResponseContinuationDeferral, a thread-static slot filled in a scope around the synchronous part of OnChunkReceivedAsync) and completes it on its own thread when one of its waits (write capacity, next receive, chunk dispatch) goes pending:
The loop's awaits use an awaiter that first registers the loop's continuation on the pending wait and then runs the deferred continuation inline. The loop no longer depends on the thread at that point, so a continuation that blocks (for example a synchronous call on the same channel) cannot stall the receive loop; this is the same exposure as a pool thread today. BlockingContinuationDoesNotStallTheLoopAsync covers it (it fails when the two steps are swapped).
Never under the gate or a lock: completions run after OnChunkReceivedAsync returned.
A thread holds at most one deferred completion. A later response completes the held one on the pool, as before this change: a continuation run inline may block until another of its requests completes, and that one must not wait behind the blocked thread (ContinuationBlockingOnTheNextResponseDoesNotDeadlockAsync).
A held completion also goes to the pool once the loop dispatched 4 more chunks without its receive going pending, so a channel whose receives keep completing synchronously does not hold a response back.
A loop that ends completes the held one on the pool, in a finally of the loop method itself (it runs on the thread that dispatched the last chunk).
A loop that resumed inside another loop's dispatch on the same thread leaves the inline completion to the outer loop, which may hold locks.
Timeouts, cancellation and Dispose still complete the wait directly.
Client thread-pool work items per request: 2 → 1.
Measurements
After the review fixes (one held completion; run 4, medians of 4 rotated rounds, vs #5078 tip):
Scenario
net11 client CPU/op
net11 throughput
net10 client CPU/op
net10 throughput
Read 1 value
43.6 → 40.5 (−7 %)
+2 %
38.7 → 35.7 (−8 %)
+3 %
Write 100
175.1 → 140.4 (−20 %)
+1 %
114.9 → 99.4 (−13 %)
+2 %
Browse
133.1 → 95.2 (−28 %)
+2 %
79.2 → 70.7 (−11 %)
−2 %
Read 100, encrypt
113.8 → 109.5 (−4 %)
+1 %
97.3 → 92.0 (−5 %)
+2 %
Read 100, encrypt, 4 cores
140.2 → 139.4 (−1 %)
0 %
113.6 → 105.1 (−7 %)
+1 %
Client work items per request 2.0 → 1.0 in every case. Earlier runs of the first version (up to 16 held completions) follow.
Load harness, 24 logical processors, 20 sessions, 12 s + 6 s warm-up, medians of 4 rotated rounds per side, runs with more than 3 cores of foreign CPU discarded. Baseline = #5078 branch tip (c1d9d6b). Client CPU in µs per operation, usr = user-mode µs/op, wi = thread-pool work items/op. "4 cores" = --client-cores 20 (server on the remaining 4 processors, encrypted).
.NET 11 (run 2 / confirming run 3):
Scenario
Client CPU/op
Client usr/op
Client wi/op
Throughput
Server CPU/op
Read 1 value
42.4 → 40.3 (−5 %) / 44.2 → 40.8 (−8 %)
−7 % / −10 %
2.0 → 1.0
+1 % / +2 %
+1 % / +3 %
Write 100
159.8 → 130.9 (−18 %)
−20 %
2.0 → 1.0
0 %
0 %
Browse
131.3 → 99.8 (−24 %) / 129.6 → 99.3 (−23 %)
−26 % / −27 %
2.0 → 1.0
+8 % / +2 %
+1 % / −3 %
Read 100, encrypt
115.2 → 108.2 (−6 %) / 123.6 → 111.6 (−10 %)
−10 % / −6 %
2.0 → 1.0
−2 % / +1 %
+4 % / −4 %
Read 100, encrypt, 4 cores
141.5 → 139.3 (−2 %) / 148.2 → 136.4 (−8 %)
+18 % / −7 %
2.0 → 1.0
+1 % / +2 %
−2 % / −2 %
.NET 10 (run 2):
Scenario
Client CPU/op
Client wi/op
Throughput
Read 1 value
37.6 → 34.9 (−7 %)
2.0 → 1.0
+1 %
Write 100
95.3 → 91.4 (−4 %)
2.0 → 1.0
+2 %
Browse
76.9 → 68.1 (−11 %)
2.0 → 1.0
+1 %
Read 100, encrypt
101.7 → 98.9 (−3 %)
2.0 → 1.0
−1 %
Read 100, encrypt, 4 cores
103.2 → 99.2 (−4 %)
2.0 → 1.0
−1 %
The encrypted scenario moves ±5 % between identical builds (A/A spread in these runs), so −2 %/−1 % there is noise; the confirming .NET 11 run was +1 %.
Dropped: server writer started at the worker's park point
The second follow-up of #5078 was implemented and measured, and is not included. BeginWriteSymmetricMessage deferred ScheduleWriter() while a request worker ran a request synchronously; the worker started the deferred writers inline when it found no request (before the fair queue spins), when a request went asynchronous, when it handed back its permit (per-request dispatch), or after 1–2 more requests, and only on threads the pool dispatched it on.
Server work items per request fell by 32–53 %, but .NET 11 server CPU per request did not follow:
It also raised client CPU per Read by 11–30 % (responses leave later and in bursts). Write kept 4 work items per request: its responses are sent after the handler went asynchronous. So the queued writer is not where the server's .NET 11 spin cost is, and deferring it costs latency and closed-loop throughput.
Tests
New ResponseContinuationDeferralTests (deferral in and out of scope, inline completion, completion on loop exit, one held completion, the 4-chunk bound, blocking continuation vs. loop, continuation blocking on the next response).
After the review fixes: Opc.Ua.Core.Tests (Stack.*) 3411 and Opc.Ua.Client.Tests 3842 passed on net10.0 and net11.0.
Opc.Ua.Core.Tests (Stack.Transport/Server/Client/Tcp/Bindings): 3409 passed on net10.0 and net11.0.
Opc.Ua.Client.Tests: 3839 passed on net10.0 and net11.0.
Opc.Ua.Server.Tests: 11034 passed; 8 failures in PhysicalFileSystemProviderConfinementTests on both runtimes, all "insufficient privileges" creating symbolic links on the local machine, unrelated to this change.
…waits
A request waits for its response on a value task source that runs its
continuation asynchronously: the receive loop completes it under the channel
gate. The source queues the continuation with preferLocal, so the loop's
thread runs it once the next receive goes pending, or a worker that the
enqueue woke takes it. When the loop's thread runs it, the woken worker found
no work, and on .NET 11 such a worker spins up to 1024 backoff units.
The receive loop now defers the completions of the responses in a chunk and
completes them on its thread when one of its waits goes pending, after it
registered its continuation on that wait. The loop then no longer depends on
the thread, so a continuation that blocks, for example a synchronous call on
the same channel, cannot stall it. At most 16 completions are deferred; a
loop that ends completes them on the pool.
Client work items per request drop from 2 to 1. Client CPU per request on
.NET 11 -5 to -24 %, on .NET 10 -3 to -11 %; throughput -2 to +8 % (within
the noise of the encrypted scenario, +1 to +2 % in the confirming run).
Starting the server writer at the request worker's park point instead of
queueing it was measured as well and dropped: server work items per request
fell by a third to a half, but CPU per request did not fall consistently and
single-value Read lost 2-5 % throughput.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
…d response
ResponseAfterChannelClosedIsDroppedQuietlyAsync completed the pending
response as soon as the client's CloseAsync returned. The client does not
wait for the server to process the close, so on a slow runner the server
channel was still open, the response was queued normally and the expected
"channel closed by the client" log never came (seen on linux net11.0 CI).
The test now waits for the listener to log the closed channel first.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
❌ Patch coverage is 83.15789% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.62%. Comparing base (f1c928f) to head (47321b2). ⚠️ Report is 12 commits behind head on master.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Critical defects in deferred-response draining can deadlock callers or leave response tasks unresolved under sustained synchronous traffic and cross-thread receive-loop cleanup.
Review fixes for the response continuation deferral:
- A thread holds one deferred completion. A later response completes the
held one on the pool: two completions drained inline in turn deadlocked
when the first continuation blocked until the second request completed.
- A held completion goes to the pool after the loop dispatched four more
chunks without its receive going pending, so a busy channel whose
receives keep completing synchronously does not hold a response back.
This replaces the cap of 16, past which the held ones stayed unsignalled.
- The loop completes a held completion in a finally of the loop method
itself, on the thread that dispatched the last chunk, not in a wrapper
that could resume on another thread.
- A loop that resumed inside another loop's dispatch leaves the inline
completion to the outer loop, which may hold locks.
Client work items per request stay at 1; client CPU per request on .NET 11
-1 to -28 %, throughput 0 to +2 %.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
A blocking buffer rent can indefinitely stall an already-received response when the buffer budget is exhausted, with neither cancellation nor the chunk-count bound able to release it.
The next receive can rent its buffer synchronously from the process
buffer budget, which blocks while the budget is exhausted. A response the
receive loop thread held stayed unsignalled meanwhile, and its caller may
be the one that returns the budget. The limiter now completes the response
held on the calling thread on the pool before it waits for capacity.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Inline receive completions can synchronously re-enter request continuations and deadlock; inline-completion nesting must be tracked, reentrant completions dispatched to the pool, and a regression test added using a default TaskCompletionSource.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Prevent nested inline receive continuations from deadlocking
If an inline request continuation completes a receive backed by a default TaskCompletionSource, the receive loop can re-enter synchronously and run a second request continuation inline. If that second continuation waits for the first caller's task, both deadlock: the pending slot has already been cleared, and t_depth does not track inline completions. Track inline-completion nesting and send reentrant completions to the pool; add a regression test whose receive source omits RunContinuationsAsynchronously.
A continuation the receive loop's thread runs inline can complete the
loop's receive synchronously (a source without asynchronous
continuations, as with an in-process peer). The loop then resumes on that
stack, defers the next response and, when it suspends, ran that one inline
too, nested in the first: a second continuation that waited for the first
never returned. The thread now counts its inline completions, and a loop
that suspends inside one completes its response on the pool.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #5078 (.NET 11 thread-pool spinning after spurious wake-ups, dotnet/runtime#137494).
Change
A request waits for its response on a
ManualResetValueTaskSourceCorewithRunContinuationsAsynchronously(the receive loop completes it under the channel gate). The BCL queues that continuation withpreferLocal: true, so either the receive loop's thread runs it once its next receive goes pending, or a worker that the enqueue woke takes it. When the loop's thread runs it, the woken worker found no work, and on .NET 11 it spins up to 1024 backoff units before it blocks.The receive loop now defers the completion of a response (
ResponseContinuationDeferral, a thread-static slot filled in a scope around the synchronous part ofOnChunkReceivedAsync) and completes it on its own thread when one of its waits (write capacity, next receive, chunk dispatch) goes pending:BlockingContinuationDoesNotStallTheLoopAsynccovers it (it fails when the two steps are swapped).OnChunkReceivedAsyncreturned.ContinuationBlockingOnTheNextResponseDoesNotDeadlockAsync).finallyof the loop method itself (it runs on the thread that dispatched the last chunk).Disposestill complete the wait directly.Client thread-pool work items per request: 2 → 1.
Measurements
After the review fixes (one held completion; run 4, medians of 4 rotated rounds, vs #5078 tip):
Client work items per request 2.0 → 1.0 in every case. Earlier runs of the first version (up to 16 held completions) follow.
Load harness, 24 logical processors, 20 sessions, 12 s + 6 s warm-up, medians of 4 rotated rounds per side, runs with more than 3 cores of foreign CPU discarded. Baseline = #5078 branch tip (c1d9d6b). Client CPU in µs per operation,
usr= user-mode µs/op,wi= thread-pool work items/op. "4 cores" =--client-cores 20(server on the remaining 4 processors, encrypted)..NET 11 (run 2 / confirming run 3):
.NET 10 (run 2):
The encrypted scenario moves ±5 % between identical builds (A/A spread in these runs), so −2 %/−1 % there is noise; the confirming .NET 11 run was +1 %.
Dropped: server writer started at the worker's park point
The second follow-up of #5078 was implemented and measured, and is not included.
BeginWriteSymmetricMessagedeferredScheduleWriter()while a request worker ran a request synchronously; the worker started the deferred writers inline when it found no request (before the fair queue spins), when a request went asynchronous, when it handed back its permit (per-request dispatch), or after 1–2 more requests, and only on threads the pool dispatched it on.Server work items per request fell by 32–53 %, but .NET 11 server CPU per request did not follow:
It also raised client CPU per Read by 11–30 % (responses leave later and in bursts). Write kept 4 work items per request: its responses are sent after the handler went asynchronous. So the queued writer is not where the server's .NET 11 spin cost is, and deferring it costs latency and closed-loop throughput.
Tests
ResponseContinuationDeferralTests(deferral in and out of scope, inline completion, completion on loop exit, one held completion, the 4-chunk bound, blocking continuation vs. loop, continuation blocking on the next response).Opc.Ua.Core.Tests(Stack.Transport/Server/Client/Tcp/Bindings): 3409 passed on net10.0 and net11.0.Opc.Ua.Client.Tests: 3839 passed on net10.0 and net11.0.Opc.Ua.Server.Tests: 11034 passed; 8 failures inPhysicalFileSystemProviderConfinementTestson both runtimes, all "insufficient privileges" creating symbolic links on the local machine, unrelated to this change.🤖 Generated with Claude Code